package com.balloon.springboot.datasource.conf;


import com.balloon.springboot.datasource.DynamicDataSource;
import com.balloon.springboot.datasource.entity.DataSourcePropertiesBean;
import com.balloon.springboot.datasource.prorperties.DefaultDataSource;
import com.balloon.springboot.datasource.prorperties.LocalDataSourceProperties;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多数据源配置器
 *
 * @author liaofuxing
 * @data 2021/9/18
 */
@EnableConfigurationProperties(LocalDataSourceProperties.class)
@Configuration
public class DataSourceConfiguration {

    private static final Logger log = LoggerFactory.getLogger(DataSourceConfiguration.class);

    @Autowired
    public LocalDataSourceProperties localDataSourceProperties;


    /**
     * 初始化数据源
     *
     * @return DataSource
     * @throws SQLException SQL 异常
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean
    public DataSource dataSource() throws SQLException {
        log.info("datasource init................");

        DataSourceProperties properties = new DataSourceProperties();
        DefaultDataSource defaultDataSource = localDataSourceProperties.getDefaultDataSource();
        if (defaultDataSource == null) {
            log.error("必须配置默认的数据源,请检查数据源配置");
            throw new SQLException("必须配置默认的数据源,请检查数据源配置");
        }
        BeanUtils.copyProperties(defaultDataSource, properties);

        // 初始化默认的数据源
        HikariDataSource defaultHikariDataSource = HikariDataSourceBuilder
                .builder()
                .setDataSourceProperties(properties)
                .build();

        DataSourceConfigRegister dataSourceConfigRegister = new DataSourceConfigRegister(localDataSourceProperties);
        List<DataSourcePropertiesBean> dataSourcePropertiesBeanList = dataSourceConfigRegister.initDataSourceProperties(defaultHikariDataSource);

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 设置从数据库中读取的数据源
        Map<Object, Object> dataSourcesMap = new HashMap<>();
        for (DataSourcePropertiesBean dataSourcePropertiesBean : dataSourcePropertiesBeanList) {
            String routeKey = dataSourcePropertiesBean.getType();
            DataSourceProperties propertiesForDBOrYml = new DataSourceProperties();
            BeanUtils.copyProperties(dataSourcePropertiesBean, propertiesForDBOrYml);

            HikariDataSource hikariDataSource = HikariDataSourceBuilder
                    .builder()
                    .setDataSourceProperties(propertiesForDBOrYml)
                    .build();
            dataSourcesMap.put(routeKey, hikariDataSource);
        }
        dynamicDataSource.setTargetDataSources(dataSourcesMap);
        dynamicDataSource.setDefaultTargetDataSource(defaultHikariDataSource);
        return dynamicDataSource;
    }

}

